import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import contants.TitleEnum;
import entity.QuestionOption;
import entity.QuestionTitle;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ReadText {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = ReadText.class.getClassLoader().getResourceAsStream("recommendedFormat.doc");
        XWPFDocument document = new XWPFDocument(inputStream);
        // 获取所有段落
        Iterator<IBodyElement> iterator = document.getBodyElements().iterator();
        // 获取所有的图片
        List<XWPFPictureData> pictures = document.getAllPictures();
        for (XWPFPictureData picture : pictures) {
            System.out.println(picture.toString());
        }
        List<QuestionTitle> titleList = new ArrayList<>();
        QuestionTitle title = null;
        while (iterator.hasNext()) {
            IBodyElement element = iterator.next();
            // 段落
            if (element instanceof XWPFParagraph) {
                String text = ((XWPFParagraph) element).getText();
                if (StrUtil.isBlank(text)) {
                    continue;
                }
                TitleEnum titleType = getTitleType(text);
                if (ObjectUtil.isNotEmpty(titleType)) {
                    // 设置 title
                    title = new QuestionTitle();
                    int i = text.indexOf(".");
                    String prefix = text.substring(0, i);
                    String suffix = text.substring(i + 1);
                    title.setId(prefix);
                    title.setType(titleType.getTitleType());
                    title.setTitleName(suffix);
                    titleList.add(title);
                } else {
                    if (text.contains("：")) {
                        int index = text.indexOf("：");
                        String prefix = text.substring(0, index);
                        String suffix = text.substring(index + 1);
                        if ("答案".equals(prefix)) {
                            title.setAnswer(suffix);
                        } else if ("答案解析".equals(prefix)) {
                            title.setResolve(suffix);
                        } else if ("难易程度".equals(prefix)) {
                            title.setDifficulty(suffix);
                        }
                    } else {
                        // 选项
                        if (text.contains(".")) {
                            int i = text.indexOf(".");
                            String prefix = text.substring(0, i);
                            String suffix = text.substring(i);
                            QuestionOption questionOption = new QuestionOption();
                            questionOption.setTitleId(title.getId());
                            questionOption.setOptionName(prefix);
                            questionOption.setOptionContent(suffix);
                            List<QuestionOption> options = title.getOptionList();
                            if (ObjectUtil.isEmpty(options)) {
                                options = new ArrayList<>();
                            }
                            options.add(questionOption);
                            title.setOptionList(options);
                        }
                    }
                }
            }
            // 表格
            if (element instanceof XWPFTable) {
                System.out.println(((XWPFTable) element).getText());
            }
            // SDF
            if (element instanceof XWPFSDT) {
                System.out.println(((XWPFSDT) element).getContent().getText());
            }
        }
        System.out.println(titleList);
    }

    private static TitleEnum getTitleType(String text) {
        return TitleEnum.checkExists(text);
    }


}
